package com.kchouse;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gxz.PagerSlidingTabStrip;
import com.kchouse.common.BaseFragment;
import com.kchouse.union.GDMapFragment;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class UnionFragment extends BaseFragment {


    private View view;

    private PagerSlidingTabStrip pst;
    private ViewPager viewPager;
    //声明pst的标题
    private ArrayList<Fragment> fragments = new ArrayList<>();
    String[] titles = {"商家列表", "地图展示"};
    private MyPagerAdapter adapter;
    private ListFragment list1;
    private boolean isfirst=true;

    public UnionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment




        if (view == null) {
            view = inflater.inflate(R.layout.fragment_union, null);
            intiData();
            intiView();
        }

        return view;
    }

    private void intiView() {
        intiPSTS();
    }

    private void intiData() {

    }

    @Override
    public void onResume() {
        super.onResume();

        Log.e("****","isfirst:"+isfirst);
        if(!isfirst){
            getefreshFrament();
            Log.e("****","onStart");
        }
        isfirst=false;
    }

    /**
     * 去刷新
     */
    public void getefreshFrament (){
        list1.fresh();
    }

    /**
     * 初始化tab
     */
    private void intiPSTS() {
        fragments.clear();
        pst = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        viewPager = (ViewPager) view.findViewById(R.id.pager);
         list1 = new ListFragment(10, "联盟");
        GDMapFragment list2 = new GDMapFragment();
//        //添加fragment到集合中时注意顺序
        fragments.add(list1);
        fragments.add(list2);
        adapter = new MyPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        pst.setViewPager(viewPager);
        viewPager.setCurrentItem(0);

        // 设置Tab是自动填充满屏幕的
        pst.setShouldExpand(true);
// 设置Tab的分割线的颜色
        pst.setDividerColor(getResources().getColor(R.color.colorred));
// 设置分割线的上下的间距,传入的是dp
        pst.setDividerPaddingTopBottom(10);
////设置Tab底部线的颜色
        pst.setUnderlineColor(getResources().getColor(R.color.bg_line));

// 设置Tab 指示器Indicator的高度,传入的是dp
        pst.setIndicatorHeight(3);
// 设置Tab Indicator的颜色
        pst.setIndicatorColor(getResources().getColor(R.color.colorred));

// 设置Tab标题文字的大小,传入的是sp
        pst.setTextSize(13);
// 设置选中Tab文字的颜色
        pst.setSelectedTextColor(getResources().getColor(R.color.colorred));
//设置正常Tab文字的颜色
        pst.setTextColor(getResources().getColor(R.color.colorblack));
//设置Tab文字的左右间距,传入的是dp
        pst.setTabPaddingLeftRight(12);

//是否支持动画渐变(颜色渐变和文字大小渐变)
        pst.setFadeEnabled(false);
// 设置最大缩放,是正常状态的0.3倍
//        pst.setZoomMax(0.3F);
        pst.setZoomMax(0);

//这是当点击tab时内容区域Viewpager是否是左右滑动,默认是true
        pst.setSmoothScrollWhenClickTab(true);
    }

    /**
     * * 自定义适配器
     */
    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }//设置每页的标题

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        //设置每一页对应的fragment
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        //fragment的数量
        @Override
        public int getCount() {
            return fragments.size();
        }
    }


}
